package com.orange.common.base.entity;


import java.io.Serializable;
import java.util.function.Consumer;

public class RequestData<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private ReqHeader header;
    private T body;

    // 私有构造函数，只能通过建造者创建实例
    private RequestData(Builder<T> builder) {
        this.header = builder.header;
        this.body = builder.body;
    }

    // Getter 方法
    public ReqHeader getHeader() {
        return header;
    }

    public T getBody() {
        return body;
    }

    @Override
    public String toString() {
        return "RequestData{" +
                "header=" + header +
                ", body=" + body +
                '}';
    }

    // 静态建造者类
    public static class Builder<T> {
        private ReqHeader header;
        private T body;

        public Builder() {
            // 默认创建一个基础header
            this.header = ReqHeader.createDefault();
        }

        // 复制构造函数
        public Builder(RequestData<T> requestData) {
            this.header = requestData.header;
            this.body = requestData.body;
        }

        public Builder<T> header(ReqHeader header) {
            this.header = header;
            return this;
        }

        // 便捷方法：通过Consumer自定义header
        public Builder<T> header(Consumer<ReqHeader.Builder> headerCustomizer) {
            ReqHeader.Builder headerBuilder = ReqHeader.builder(this.header);
            headerCustomizer.accept(headerBuilder);
            this.header = headerBuilder.build();
            return this;
        }

        public Builder<T> body(T body) {
            this.body = body;
            return this;
        }

        public RequestData<T> build() {
            // 可以在此处添加验证逻辑
            if (header == null) {
                throw new IllegalStateException("Header cannot be null");
            }
            return new RequestData<>(this);
        }
    }

    // 静态方法：从现有实例创建建造者
    public static <T> Builder<T> from(RequestData<T> requestData) {
        return new Builder<>(requestData);
    }

    // 静态方法：创建空建造者
    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    // 静态方法：快速创建带有body的请求
    public static <T> RequestData<T> of(T body) {
        return RequestData.<T>builder().body(body).build();
    }

    // 静态方法：快速创建带有body和自定义header的请求
    public static <T> RequestData<T> of(T body, Consumer<ReqHeader.Builder> headerCustomizer) {
        return RequestData.<T>builder().body(body).header(headerCustomizer).build();
    }

    public static void main(String[] args) {
        // 示例1: 最简单的使用方式 - 只需提供body
        RequestData<String> request1 = RequestData.of("Hello World");
        System.out.println("示例1: " + request1);

        // 示例2: 带有自定义header的请求
        RequestData<String> request2 = RequestData.of(
                "Custom data",
                header -> header.ip("192.168.1.100").requestSequenceNo("CUSTOM-123")
        );
        System.out.println("示例2: " + request2);

        // 示例3: 使用建造者模式完整构建
        RequestData<String> request3 = RequestData.<String>builder()
                .body("Detailed request")
                .header(header -> header
                        .ip("10.0.0.1")
                        .requestTimestamp(1234567890L)
                        .requestSequenceNo("SEQ-001"))
                .build();
        System.out.println("示例3: " + request3);

        // 示例4: 从现有请求复制并修改
        RequestData<String> request4 = RequestData.from(request3)
                .body("Modified body")
                .header(header -> header.ip("192.168.0.55"))
                .build();
        System.out.println("示例4: " + request4);

        // 示例5: 直接使用ReqHeader对象
        ReqHeader customHeader = ReqHeader.builder()
                .ip("172.16.0.10")
                .requestSequenceNo("DIRECT-001")
                .build();

        RequestData<String> request5 = RequestData.<String>builder()
                .header(customHeader)
                .body("Direct header usage")
                .build();
        System.out.println("示例5: " + request5);
    }
}
